package peer

import (
	"fmt"
	"sync"
)

type SessionManager struct {
	sessionList sync.Map // 使用Id关联会话

	connIDGen int64 // 记录已经生成的会话ID流水号

	count int64 // 记录当前在使用的会话数量

	performer  IPerformer
	Register   chan *Session
	Unregister chan int64
}

func NewSessionMgr(performer IPerformer) *SessionManager {
	sm := &SessionManager{
		sessionList: sync.Map{},
		connIDGen:   0,
		count:       0,
		performer:   performer,
		Register:    nil,
		Unregister:  nil,
	}
	return sm
}

// 添加新的Session
func (sm *SessionManager) Add(sess *Session) {
	sm.sessionList.Store(sess.GetId(), sess)
}

func (sm *SessionManager) Delete(id int64) {
	sm.sessionList.Delete(id)
}

/**
 * 处理接收的消息
 * @param id.sessionId
 * @param msg 接收的数据
 */
func (sm *SessionManager) ProcessMessage(id int64, msg []byte) {
	fmt.Printf("SessionManager 接收消息. id:%d, msg:%v\n", id, msg)
	//sm.performer.OnReceive(nil, nil)
}